package com.example.guethe.fireanalysisapp.base;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.example.guethe.fireanalysisapp.R;
import com.example.guethe.fireanalysisapp.data.beans.BaseListResponse;
import com.example.guethe.fireanalysisapp.data.beans.CataLog;
import com.example.guethe.fireanalysisapp.data.beans.Device;
import com.example.guethe.fireanalysisapp.fragments.CatalogFragment;
import com.example.guethe.fireanalysisapp.widget.StateView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.base.SimpleRecAdapter;
import cn.droidlover.xdroidmvp.base.XFragmentAdapter;
import cn.droidlover.xdroidmvp.mvp.XLazyFragment;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xrecyclerview.XRecyclerContentLayout;
import cn.droidlover.xrecyclerview.XRecyclerView;

/**
 * Created by 何建钦 on 2018/3/27.
 */

public abstract class BaseHomeFragment extends XLazyFragment<BaseHomePresenter> {

    @BindView(R.id.contentLayout)
    XRecyclerContentLayout contentLayout;



    StateView errorView;

    protected static final int MAX_PAGE = 10;


    @Override
    public void initData(Bundle savedInstanceState) {

        initAdapter();
        if(getType()==""){
            getP().loadData( 1);
        }else {
            getP().loadData( 1,getType());
        }
    }

    private void initAdapter() {
        setLayoutManager(contentLayout.getRecyclerView());
        contentLayout.getRecyclerView()
                .setAdapter(getAdapter());
        contentLayout.getRecyclerView()
                .setOnRefreshAndLoadMoreListener(new XRecyclerView.OnRefreshAndLoadMoreListener() {
                    @Override
                    public void onRefresh() {
                        if(getType()=="") {
                            getP().loadData(1);
                        }else {
                            getP().loadData( 1,getType());
                        }
                    }

                    @Override
                    public void onLoadMore(int page) {
                        if(getType()=="") {
                            getP().loadData(page);
                        }else {
                            getP().loadData(page,getType());
                        }

                    }
                });


        if (errorView == null) {
            errorView = new StateView(context);
        }
        contentLayout.errorView(errorView);
        contentLayout.loadingView(View.inflate(getContext(), R.layout.view_loading, null));

        contentLayout.getRecyclerView().useDefLoadMoreView();
    }


    public abstract SimpleRecAdapter getAdapter();

    public abstract void setLayoutManager(XRecyclerView recyclerView);

    public abstract String getType();

    public void showError(NetError error) {
        if (error != null) {
            switch (error.getType()) {
                case NetError.ParseError:
                    errorView.setMsg("数据解析异常");
                    break;

                case NetError.AuthError:
                    errorView.setMsg("身份验证异常");
                    break;

                case NetError.BusinessError:
                    errorView.setMsg("业务异常");
                    break;

                case NetError.NoConnectError:
                    errorView.setMsg("网络无连接");
                    break;

                case NetError.NoDataError:
                    errorView.setMsg("数据为空");
                    break;

                case NetError.OtherError:
                    errorView.setMsg("其他异常");
                    break;
            }
            contentLayout.showError();
        }
    }

    public void showData(int page, BaseListResponse<CataLog> data) {
        if (page > 1) {
            getAdapter().addData(data.getData());
        } else {
            getAdapter().setData(data.getData());
        }

        contentLayout.getRecyclerView().setPage(page, MAX_PAGE);

        if (getAdapter().getItemCount() < 1) {
            contentLayout.showEmpty();
            return;
        }
    }

    public void showData(int page, BaseListResponse<Device> data,String type) {
        if (page > 1) {
            getAdapter().addData(data.getData());
        } else {
            getAdapter().setData(data.getData());
        }

        contentLayout.getRecyclerView().setPage(page, MAX_PAGE);

        if (getAdapter().getItemCount() < 1) {
            contentLayout.showEmpty();
            return;
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_base_home;
    }

    @Override
    public BaseHomePresenter newP() {
        return new BaseHomePresenter();
    }
}
